<template>
  <el-card class="publish-container">
    <div slot="header" class="clearfix">
      <breadcrum>发布文章</breadcrum>
    </div>
    <el-form ref="form" :model="form" :rules='rules'   label-width="80px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <quill-editor v-model="form.content" :options="editorOption"></quill-editor>
      </el-form-item>
      <el-form-item label="封面">
        <el-radio-group v-model="form.coverType" @change="chooseType">
          <el-radio :label="1">单图</el-radio>
          <el-radio :label="0">无图</el-radio>
          <el-radio :label="3">三图</el-radio>
          <el-radio :label="-1">自动</el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- 引入封面组件 -->
      <el-row v-if ='form.coverType>0'>
        <el-col
        v-for ='(item, index) in form.coverType'
        :key="item"
        :span="6"><my-cover v-model ='form.coverImages[index]'/></el-col>
      </el-row>

      <!-- 引入频道组件 -->
      <my-channel v-model="form.channelId"></my-channel>
      <el-form-item>
        <el-button
        style="margin-right: 50px"
        :disabled='isLoading'
        :loading="isLoading"
        type="primary"
        @click="pubArticle(false)">发表</el-button>

        <el-button
        :disabled='isLoading'
        :loading="isLoading"
        type="primary"
        @click="depDraft(true)">存入草稿</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
// 引入插件的css样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入组件,注意加大括号
import { quillEditor } from 'vue-quill-editor'
import MyCover from '../../components/myCover.vue'

export default {
  name: 'Article',
  components: {
    quillEditor,
    MyCover
  },
  data () {
    return {
      form: {
        coverImages: [],
        coverType: 0,
        channelId: null,
        content: '当我第一次看见你的时候，我就想一直对你好',
        title: '我想我是海'
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '内容', trigger: 'blur' },
          { min: 20, max: 3000, message: '长度在 20 到 3000 个字符', trigger: 'blur' }
        ],
        channelId: [
          { required: true, message: '请选择频道', trigger: 'blur' }
        ]
      },
      // 富文本配置对象
      editorOption: {
        // 占位配置
        placeholder: '当我第一次看见你的时候，我就想一直对你好',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      },
      articles: [],
      total_count: 0,
      page: 1,
      isLoading: false
    }
  },
  methods: {
    // 发布文章
    pubArticle (isDraft) {
      this.isLoading = true
      this.getArticle(isDraft)
    },
    depDraft (isDraft) {
      this.isLoading = true
      this.getArticle(isDraft)
    },
    // 获取文章数据
    async getArticle (isDraft) {
      // 设计查询条件：title、channel_id、content、cover
      const { title, content } = this.form
      try {
        const res = await this.$axios({
          url: '/mp/v1_0/articles',
          method: 'post',
          params: {
            draft: isDraft
          },
          data: {
            title,
            content,
            channel_id: this.form.channelId,
            cover: {
              type: this.form.coverType,
              images: this.form.coverImages
            }
          }
        })
        this.$message.success('获取文章信息成功')
        console.log(res)
      } catch (err) {
        this.$message.error('获取文章信息失败')
        console.log(err)
      }
      this.isLoading = false
    },
    // 当前页码数据传输
    pageChange (currentPage) {
      console.log('当前页码是', currentPage)
      this.page = currentPage
      this.getArticle()
    },
    // 随意选择封面时，存储封面的数据处理
    chooseType () {
      if (this.form.coverType === 1) {
        this.form.coverImages.length = 1
      } else if (this.form.coverType === 0) {
        this.form.coverImages.length = 0
      }
    }
  }
}
</script>

<style lang='less' scoped>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }
  .publish-container /deep/ .ql-editor{
  height: 300px;
  }
  .publish-container /deep/ .ql-toolbar.ql-snow{
    padding: 0 8px;
  }
</style>
